package com.hnucm.cooklife.ui.fragment;

import android.content.Context;
import android.content.Intent;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.GridSpanSizeLookup;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.hnucm.cooklife.R;
import com.hnucm.cooklife.adapter.ClasAdapter;
import com.hnucm.cooklife.adapter.RecomAdapter;
import com.hnucm.cooklife.base.BaseFragment;
import com.hnucm.cooklife.base.BasePresenter;
import com.hnucm.cooklife.base.utils.Constant;
import com.hnucm.cooklife.bean.base.Event;
import com.hnucm.cooklife.bean.db.ClasSec;
import com.hnucm.cooklife.bean.home.ClassData;
import com.hnucm.cooklife.contract.clas.Contract;
import com.hnucm.cooklife.presenter.home.ClasPresenter;
import com.hnucm.cooklife.ui.activity.DishResultActivity;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class ClassiFragment extends BaseFragment<Contract.IClasView, ClasPresenter> implements Contract.IClasView {

    private Context mContext;
    private ClasAdapter mClasAdapter;
    private List<ClasSec> mClasSecList= new ArrayList<>();

    @BindView(R.id.clas_recycler)
    RecyclerView mRecyclerView;

    @Override
    protected int getContentViewId() {
        return R.layout.classi_fragment;
    }

    @Override
    protected void init() {
        mContext = getActivity().getApplicationContext();
        initAdapter();
        mPresenter.loadClas();
    }

    private void initAdapter() {
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(), 4);
        mRecyclerView.setLayoutManager(gridLayoutManager);
        mClasAdapter = new ClasAdapter(R.layout.class_item,R.layout.clas_head_item,null);
        mClasAdapter.setContext(mContext);
        mRecyclerView.setAdapter(mClasAdapter);
        mClasAdapter.setOnItemClickListener(new RecomAdapter.onItemClickListener() {
            @Override
            public void onSeleClickListener(View view, int position) {
                for (int i = 0; i < mClasSecList.size(); i++) {
                    System.out.println(mClasSecList.get(position));
                }
                ClassData.DataDTO clas = (ClassData.DataDTO) mClasSecList.get(position).getObject();

                Intent intent = new Intent(getActivity(), DishResultActivity.class);
                intent.putExtra(Constant.KEY_KEYWORD,clas.getClaname());
                startActivity(intent);
            }
        });

//        mClasAdapter.setOnItemClickListener(new OnItemClickListener() {
//            @Override
//            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
//                ClasSec clasSec = mClasSecList.get(position);
//                Intent intent = new Intent(getActivity(), DishResultActivity.class);
//                intent.putExtra(Constant.CLAS_KEYWORD,clasSec.claname);
////                startActivity(intent);
//            }
//        });
    }

    @Override
    protected ClasPresenter createPresenter() {
        return new ClasPresenter();
    }

    @Override
    public void onLoadClas(List<ClasSec> clasList) {
        mClasSecList.clear();
        mClasSecList.addAll(clasList);
        mClasAdapter.addData(mClasSecList);
    }

    @Override
    public void onRefreshClas(List<ClasSec> clasList) {

    }

    public void startLoadingView() {
        Event e = new Event();
        e.target = Event.TARGET_MAIN;
        e.type = Event.TYPE_START_ANIMATION;
        EventBus.getDefault().post(e);
    }

    public void stopLoadingView() {
        Event e = new Event();
        e.target = Event.TARGET_MAIN;
        e.type = Event.TYPE_STOP_ANIMATION;
        EventBus.getDefault().post(e);
    }


    @Override
    public void onLoading() {
        startLoadingView();
    }

    @Override
    public void onLoadingFailed() {

    }

    @Override
    public void onLoadSuccess() {
        stopLoadingView();
    }
}
